package game.tictactoe.util;

import game.tictactoe.pojo.Player;


public class Utils
{
	private static String arrayIndexLocator[] = {"0,0","0,1","0,2","1,0","1,1","1,2","2,0","2,1","2,2"};
	private static String location[] = {"upper left","upper middle","upper right","middle left","center","middle right","lower left","lower middle","lower right"};
	
	public static String getArrayIndexFromString(int input)
	{
		if(input > 0 && input < 10)
		{
			for(int i=0; i< 9; i++)
			{
				if(input == (i+1))
				{
					return  arrayIndexLocator[i];
				}
			}
		}
		return null;
	}

	public static String getLocationString(int input)
	{
		for(int i=0; i< 9; i++)
		{
			if(input == (i+1))
			{
				return  location[i];
			}
		}
		return null;
	}

	public static int getMoveFromArrayIndices(int[] arrayIndices)
	{
		int move = -1;
		String locationString = arrayIndices[0]+","+arrayIndices[1];
		for(int i=0; i< arrayIndexLocator.length; i++)
		{
			if(arrayIndexLocator[i].equals(locationString))
			{
				move = i+1;
			}
		}
		return move;
	}
	
	/**
	 * This method constructs the output message for the UI describing the recent moves. 
	 * @param inputValue1
	 * @param inputValue2
	 * @param gameStatus
	 * @return
	 */
	public static String constructMessage(int inputValue1, int inputValue2, int gameStatus)
	{
		StringBuilder sb = new StringBuilder();
		sb. append("You have put an X in the ");
		sb. append(getLocationString(inputValue1)+".");
		switch(gameStatus)
		{
			case 1:
				sb.append("I'm the Winner.");
				break;
			case -1:
				sb.append("You are the Winner.");
				break;
			case 0:
				sb.append("Game is a tie.");
				break;
			case -2:
				sb.append("My Move is O in the ");
				sb.append(getLocationString(inputValue2));
		}		
		return sb.toString();
	}
	
	/**
	 * This method constructs the output message for the UI describing the recent moves. This can be used for two AIPlayer or HumanPlayers.
	 * @param inputValue1
	 * @param inputValue2
	 * @param gameStatus
	 * @return
	 */
	public static String constructMessage(Player player1, int inputValue1, Player player2, int inputValue2, int gameStatus)
	{
		StringBuilder sb = new StringBuilder();
		sb. append(player1.getMySeed()+ " move is in the ");
		sb. append(getLocationString(inputValue1)+".");
		switch(gameStatus)
		{
			case 1:
				sb.append(player1.getMySeed()+ " is the Winner.");
				break;
			case -1:
				sb.append(player2.getMySeed()+" is the Winner.");
				break;
			case 0:
				sb.append("Game is a tie.");
				break;
			case -2:
				sb.append(player2.getMySeed()+" move is in the ");
				sb.append(getLocationString(inputValue2));
		}		
		return sb.toString();
	}
}
